package org.grc.app.z_project.z_framework;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.naming.ldap.Rdn;

public class RandomClass {
	static int[][] table = new int[9][9];

	private static void outPut() {
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				System.out.print(table[i][j] + " ");
			}
			System.out.println();
		}
	}

	private static boolean check(int a[][], int test, int x, int y) {
		boolean flag1 = false;
		boolean flag2 = false;
		boolean flag3 = true;
		int xx=x;
		int yy=y;
		for (; xx >= 0; xx--) {

			if (test == a[xx][y]) {
				flag1 = false;
				break;
			} else {
				flag1 = true;

			}
		}
		for (; yy >= 0; yy--) {

			if (test == a[x][yy]) {
				flag2 = false;
				break;
			} else {
				flag2 = true;
			}
		}
		//	for (int i = 0; i < 3; i++) {
		//	    for (int j = 0; j < 3; j++) {
		//		if (test == a[x / 3 * 3 + i][y / 3 * 3 + j]) {
		//		    flag3= false;
		//		    break;
		//		} else {
		//		    flag3 = true;
		//		}
		//	    }
		//	    if (flag3==false){
		//		break;
		//	    }
		//	}
		return (flag1 && flag2 && flag3);
	}

	private static void printSet (Set<Integer> set) {
		System.out.print(set.toString());
	}

	private static boolean isContains(int randomNum, Set<Integer> set){
		if (set.contains(randomNum)){
			return true;
		}else{
			set.add(randomNum);
			return false;
		}
	}

	private static void test1 () {
		
		int max = 3;
		
		List<Set<Integer>> rowSetList = new ArrayList<Set<Integer>>();
		for (int i=0 ; i<max ; i++) {
			rowSetList.add(new HashSet<Integer>());
		}
		List<Set<Integer>> colSetList = new ArrayList<Set<Integer>>();
		for (int i=0 ; i<max ; i++) {
			colSetList.add(new HashSet<Integer>());
		}

		Random random = new Random();
		for (int i = 0; i < max; i++) {
			for (int j = 0; j < max; j++) {
				int randomNum=0;
				boolean flag = true;
				while (flag) {
					
					randomNum=random.nextInt(max)+1;
					flag = isContains(randomNum, colSetList.get(i)) || isContains(randomNum, rowSetList.get(j));
//					flag = false;
				}
				System.out.print(randomNum);
			}
			System.out.println();
		}
	}
	
	/*
	 * j
	 * ---------------------
	 * | |1|2|3|4|5|6|7|8|9|
	 * ---------------------
	 * |1|
	 * ---------------------
	 * |2|
	 * ---------------------
	 * |3
	 * ---------------------
	 * |4
	 * ---------------------
	 */
	
	private static void test2 () {

		Random random = new Random();
		int[] b=new int[9];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				int randomNum=random.nextInt(9)+1;
				boolean flag=false;
				Set<Integer> set=new HashSet<Integer>();
				while ( flag== false) {
					randomNum=random.nextInt(9)+1;
					if (isContains(randomNum,set)){
						flag=check(table, randomNum, i, j);
					}
				}
				table[i][j] = randomNum;
			}
		}
		outPut();
	}

	public static void main(String[] args) {
		test1();
	}
}
